package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TSMchntBindInfoMapper;
import cc.rengu.igas.momp.common.entity.MchntBindInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 用户绑定信息表接口实现类
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2020/7/10 15:10
 */

public class TSMchntBindInfoMapperImpl implements TSMchntBindInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    /**
     * 查询用户绑定信息
     * 0-成功，其它-失败
     *
     * @param instId 机构标识
     * @param userId 用户标识
     * @return cc.rengu.igas.momp.common.entity.MchntBindInfo
     * @throws Exception 异常信息
     */
    public MchntBindInfo selectMchntNoByInstIdAndUserId(String instId, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMchntNoByInstIdAndUserId");

        MchntBindInfo mchntBindInfo = new MchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setBindUserId(userId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBindInfo, mchntBindInfo);
        if (0 != iReturnCode) {
            rglog.error("根据查询条件<instId:{},userId:{}获取用户绑定信息失败,iReturnCode:<{}>", instId, userId, iReturnCode);
            return null;
        }
        return mchntBindInfo;
    }
}
